package com.yoomiito.app.ui.order.list

import android.content.Intent
import android.os.Bundle
import android.view.View

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.yoomiito.app.Constant
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseLazyFragment
import com.yoomiito.app.model.bean.OrderInfo
import com.yoomiito.app.model.bean.OrderList
import com.yoomiito.app.ui.order.detail.OrderInfoActivity
import com.yoomiito.app.utils.AllCode
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.EventMessage
import com.yoomiito.app.utils.L
import com.yoomiito.app.widget.SpacingDecoration
import com.yoomiito.app.widget.state.ContentType
import com.yoomiito.app.widget.state.EmptyType
import com.yoomiito.app.widget.state.LoadingType
import com.yoomiito.app.widget.state.StatusManager

import cn.droidlover.xdroidmvp.router.Router
import kotlinx.android.synthetic.main.fm_rcy.*

class OrderFragment : BaseLazyFragment<OrderFmPresenter>() {
    internal var currentPage = 1
    private var mType = AllCode.TaoBao
    private var orderAdapter: BaseQuickAdapter<OrderInfo, BaseViewHolder>? = null
    private var key: Int = 0
    private var isRefresh: Boolean = false

    override fun getLayoutId(): Int {
        return 0
    }

    override fun getLayoutView(): View? {
        mStatusManager = StatusManager.Builder(context)
                .addType(ContentType(R.layout.fm_rcy))
                .addType(LoadingType())
                .addType(EmptyType().setEmptyContent("未购买商品"))
                .build()
        return mStatusManager.view()
    }

    override fun newP(): OrderFmPresenter {
        return OrderFmPresenter(App.mAppConfig)
    }

    override fun initData_kotlin(view: View?, savedInstanceState: Bundle?) {
        val v0 = view?.findViewById<androidx.recyclerview.widget.RecyclerView>(R.id.fm_rcy)
        val bundle = arguments
        val type = bundle?.getInt(Constant.TYPE)
        key = bundle?.getInt(Constant.KEY)!!

        when (type) {
            AllCode.TaoBao -> mType = 1
            AllCode.Gift -> mType = 2
            AllCode.BaoPin -> mType = 3
            AllCode.YouPin -> mType = 4
            AllCode.College -> mType = 5
            AllCode.StarStore -> mType = 6
        }
        L.i("订单0：$fm_refreshLayout  $fm_rcy")
        L.i("订单1：$v0")

//        initRcy()
//        initRefreshLayout()
    }

    override fun eventMessage(eventMessage: EventMessage) {
        L.i("订单列表收到：" + eventMessage.eventType + "   " + eventMessage.goodsType)
        if (AllCode.UPDATE_DATA == eventMessage.eventType) {//只刷新当前界面数据
            if (eventMessage.goodsType == mType) {
                fm_refreshLayout.autoRefresh()
            }
        }
    }

    private fun initRefreshLayout() {
        fm_refreshLayout.setOnRefreshListener { refreshLayout ->
            currentPage = 1
            p!!.queryOrderList(mType, currentPage)
        }
        fm_refreshLayout.setOnLoadMoreListener { refreshLayout ->
            currentPage++
            p!!.queryOrderList(mType, currentPage)
        }
        fm_refreshLayout.autoRefresh()
    }

    private fun initRcy() {
        fm_rcy.setPadding(DensityUtil.dp2px(12f), 0, DensityUtil.dp2px(12f), 0)
        val manager = androidx.recyclerview.widget.LinearLayoutManager(context, androidx.recyclerview.widget.LinearLayoutManager.VERTICAL, false)
        fm_rcy.layoutManager = manager
        if (mType == 3) {
            fm_rcy.addItemDecoration(SpacingDecoration(0, DensityUtil.dp2px(10f), false))

            orderAdapter = BaoPinOrderAdapter(null)
            (orderAdapter as BaoPinOrderAdapter).onParentChildClickListener = { adapter, view, childPosition, parentPosition ->
                val orderInfo = orderAdapter!!.data[parentPosition]
                if (key == Constant.CODE_GET_ORDER_INFO_REQUEST_ID) {//发布圈子时返回的订单信息
                    val orderGoods = (adapter as BaoPinOrderGoodsAdapter).data
                    val goods = orderGoods[childPosition]
                    val orderActivity = activity as OrderActivity?
                    val intent = Intent()
                    intent.putExtra(Constant.KEY_ORDER_ID, goods.order_id.toString())
                    intent.putExtra(Constant.KEY_PRODUCT_ID, goods.product_id.toString())
                    //                    intent.putExtra(Constant.KEY_ORDER_TYPE,mType);
                    intent.putExtra(Constant.KEY_ORDER_TYPE, goods.order_type)
                    intent.putExtra(Constant.KEY_TITLE, goods.item_title)
                    orderActivity!!.setResult(Constant.CODE_GET_ORDER_INFO_RESULT_ID, intent)
                    orderActivity.finish()
                } else {
                    if (mType != 1) {
                        Router.newIntent(context)
                                .putString(Constant.ID, orderInfo.order_id.toString())
                                .putInt(Constant.TYPE, orderInfo.order_type)
                                .to(OrderInfoActivity::class.java)
                                .launch()
                    }
                }
            }
        } else {
            orderAdapter = OrderAdapter(null, key,mType)
        }
        fm_rcy.adapter = orderAdapter
        orderAdapter!!.setOnItemClickListener { adapter, view, position ->
            val orderInfo = orderAdapter!!.data[position]
            if (key == Constant.CODE_GET_ORDER_INFO_REQUEST_ID) {//发布圈子时返回的订单信息
                val orderActivity = activity as OrderActivity?
                val intent = Intent()
                intent.putExtra(Constant.KEY_ORDER_ID, orderInfo.order_id.toString())
                intent.putExtra(Constant.KEY_PRODUCT_ID, orderInfo.product_id)
                intent.putExtra(Constant.KEY_ORDER_TYPE, orderInfo.order_type)
                intent.putExtra(Constant.KEY_TITLE, orderInfo.item_title)
                orderActivity!!.setResult(Constant.CODE_GET_ORDER_INFO_RESULT_ID, intent)
                orderActivity.finish()
            } else {
                if (mType != 1) {
                    Router.newIntent(context)
                            .putString(Constant.ID, orderInfo.order_id.toString())
                            .putInt(Constant.TYPE, orderInfo.order_type)
                            .to(OrderInfoActivity::class.java)
                            .launch()
                }
            }
        }
    }

    fun setData(orderList: OrderList) {
        isRefresh = true
        if (currentPage == 1) {
            if (orderList.data == null || orderList.data.size == 0) {
                fm_refreshLayout.finishLoadMoreWithNoMoreData()
                mStatusManager.showEmpty()
            } else {
                mStatusManager.showContent()
                orderAdapter!!.setNewData(orderList.data)
            }
            fm_refreshLayout.finishRefresh(true)
        } else {
            if (orderList.data == null || orderList.data.size == 0) {
                fm_refreshLayout.finishLoadMoreWithNoMoreData()
            } else {
                orderAdapter!!.addData(orderList.data)
            }
            fm_refreshLayout.finishLoadMore(true)
        }
    }

    fun finishRefresh() {
        fm_refreshLayout.finishRefresh(true)
        fm_refreshLayout.finishLoadMore(true)
    }

}
